## ==================================================
## 
##               SCRIPT TO BPSR
## 
## ==================================================
 
# --------------------------
# install and load packages

if(require(tidyverse) == F) {install.packages("tidyverse"); require(tidyverse)}
if(require(janitor) == F) {install.packages("janitor"); require(janitor)}
if(require(rio) == F) {install.packages("rio"); require(rio)}
if(require(electoral) == F) {install.packages("electoral"); require(electoral)}
if(require(formattable) == F) {install.packages("formattable"); require(formattable)}
if(require(ggalt) == F) {install.packages("ggalt"); require(ggalt)}

# -----------------------
# set working directory

setwd("directory_name")

# --------------------------
# Load data

base_partidos <- import("bpsr_data.csv")

## ===========================================
## Descriptive Statistics

# --------------
# Figure 1

base_partidos %>% 
  group_by(org_superior, DAS_n, filiado) %>% 
  summarise(n = n()) %>% mutate(freq = n/sum(n)) %>% 
  filter(filiado == 1) %>% 
  ungroup() %>% 
  group_by(DAS_n) %>% 
  mutate(media_ba = mean(freq)) %>% 
  mutate(org_superior = fct_relevel(org_superior, 
                                    "MDA", "MTB", "MTUR", 
                                    "MCID", "MDS", "MS", 
                                    "ME", "MDH",
                                    "MEC", "MINC", "MIN", "MJ",
                                    "MAPA", "MMA", "PR", "MPA",
                                    "MDIC", "MC", "MCTIC", "MME",
                                    "MP", "MT", "MPS", "MD",
                                    "MF", "MRE")) %>% 
  mutate(DAS_n = ifelse(DAS_n == 0, "% filiados no médio escalão (DAS 1/4)  ", "% filiados no alto escalão (DAS 5/6)  ")) %>% 
  ggplot(aes(org_superior, freq, fill = factor(DAS_n))) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values=c("grey21", "grey59")) +
  theme_bw() + theme(legend.position ="bottom", 
                     legend.title = element_blank(),
                     plot.caption = element_text(hjust = 0),
                     text = element_text(size = 13)) +
  labs(x = "", y = "", 
       caption = "Nota: A linha horizontal cinza indica a média de filiados no alto escalão, enquanto a linha horizontal preta representa a média de filiados no médio escalão.") + 
  scale_y_continuous(limits = c(0,0.41), expand = c(0,0),
                     labels = scales::percent_format(accuracy = 1)) +
  geom_hline(yintercept = 0.13, size = 1.2) +
  geom_hline(yintercept = 0.21, size = 1.2,
             color = "grey59") 

ggsave("fig1.pdf")  

## --------------
## Figure 2 

base_partidos %>% 
  filter(DAS_n == 0) %>% # DAS 1 a 4
  group_by(org_superior, ano, filiado) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(org_superior, ano) %>%
  mutate(freq = n/sum(n)) %>% 
  ungroup %>% 
  group_by(org_superior) %>% 
  filter(filiado == 1 & !org_superior %in% c("MDH", "MPA")) %>%
  print(n=100)%>%
  ggplot(aes(ano, freq, group = org_superior)) +
  geom_line(size = 0.5) +
  facet_wrap(~org_superior, scales = "free_x") +
  theme_bw() +
  labs(x = "", y = "")

ggsave("fig2.pdf")

## -------------------
## Figure 3 

base_partidos %>% 
  filter(DAS_n == 1) %>% # DAS 5 a 6
  group_by(org_superior, ano, filiado) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(org_superior, ano) %>%
  mutate(freq = n/sum(n)) %>% 
  ungroup %>% 
  group_by(org_superior) %>% 
  filter(filiado == 1 & !org_superior %in% c("MDH", "MPA")) %>%
  print(n=100)%>%
  ggplot(aes(ano, freq, group = org_superior)) +
  geom_line(size = 0.5) +
  facet_wrap(~org_superior, scales = "free_x") +
  theme_bw() +
  labs(x = "", y = "")

ggsave("fig3.pdf")

## ------------------------
## Figure 4


base_partidos %>% 
  filter(filiado == 1, DAS_n == 1&
           !org_superior %in% c("MDH", "MPA")) %>% 
  group_by(org_superior, sigla_partido) %>% 
  arrange(org_superior) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(org_superior) %>%
  mutate(prop = n/sum(n)) %>% 
  summarise(NP_index = enp(prop,  "Golosov")) %>% 
  ggplot(., aes(reorder(org_superior, NP_index), NP_index)) +
  geom_lollipop(point.size = 1.5) + coord_flip() +
  scale_y_continuous(expand = c(0,0), 
                     limits = c(0,6.5)) +
  labs(x = "", y = "") + theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.ticks = element_blank())

ggsave("fig4.pdf")

## ---------------------------
## Figure 5

frac_min_56 <- base_partidos %>% 
  filter(filiado == 1, DAS_n == 1) %>% 
  group_by(ano, sigla_partido) %>% 
  arrange(ano) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(ano) %>%
  mutate(prop = n/sum(n)) %>% 
  summarise(NP_index = enp(prop,  "Golosov")) %>% 
  select(ano, NP_index)

write_csv(frac_min_56, "frac_min_56.csv")

frac_min_14 <- base_partidos %>% 
  filter(filiado == 1, DAS_n == 0) %>% 
  group_by(ano, sigla_partido) %>% 
  arrange(ano) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(ano) %>%
  mutate(prop = n/sum(n)) %>% 
  summarise(NP_index14 = enp(prop,  "Golosov")) %>% 
  select(ano, NP_index14)

write_csv(frac_min_14, "frac_min_14.csv")

frac_min_14 %>% 
  left_join(frac_min_56) %>% 
  gather(., 'key', "rae", -ano) %>% 
  mutate(key = ifelse(key == "NP_index14", "DAS 1-4    ", "DAS 5-6")) %>% 
  ggplot(aes(ano, rae, group = key, color = key)) +
  geom_line(size = 0.7) + theme_bw()  +
  labs(x = "", y = "") +
  scale_x_continuous(breaks = c(1999, 2003, 2007, 2011, 2015)) +
  theme(legend.position = "bottom", 
        legend.title = element_blank()) +
  scale_color_manual(values = c("darkblue", "indianred"))

## -------------------------
## Figure 6

base_partidos %>% 
  filter(filiado == 1, DAS_n == 1) %>% 
  group_by(org_superior, sigla_partido, ano) %>% 
  arrange(org_superior) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(org_superior, ano) %>%
  mutate(prop = n/sum(n)) %>% 
  summarise(NP_index = enp(prop,  "Golosov")) %>%  
  filter(!org_superior %in% c("MDH", "MPA")) %>%
  arrange(-NP_index) %>% 
  ggplot(aes(ano, NP_index)) +
  geom_line()+ theme_bw() +
  facet_wrap(~org_superior) +
  labs(x = "", y = "")+
  scale_y_continuous(
    labels = scales::number_format(accuracy = 0.1)) +
  scale_x_continuous(breaks = c(2003,2009, 2017)) 

ggsave("fig6.pdf")

## --------------------------
## Figure 7

filiado_per <- base_partidos %>% 
  filter(DAS_n == 1) %>% 
  group_by(org_superior, filiado) %>% 
  summarise(n = n()) %>% 
  mutate(freq_fil = n/sum(n)) %>% 
  arrange(-freq_fil) %>% 
  filter(filiado == 1)

frag_per <-base_partidos %>% 
  filter(filiado == 1, DAS_n == 1) %>% 
  group_by(org_superior, ano, sigla_partido) %>% 
  summarise(n = n()) %>% 
  ungroup %>% 
  group_by(org_superior, ano) %>%
  mutate(prop = n/sum(n)) %>% 
  mutate(NP_index = enp(prop,  "Golosov")) %>% 
  ungroup %>% 
  group_by(org_superior) %>% 
  summarise(media = mean(NP_index)) %>% 
  arrange(-media)

filiado_per %>% 
  left_join(frag_per, by = "org_superior") %>% 
  select(-c(n, filiado)) %>%  
  ggplot(aes(freq_fil, media)) + 
  geom_point() + ggrepel::geom_text_repel(aes(label=org_superior)) +
  theme_bw() + scale_y_continuous(limits = c(0.5,4.2)) +
  labs(x = "Partidarização", y = "Fracionalização")

ggsave("fig7.pdf")

